<template>
  <div class="add_goods">
    <el-form :model="addForm" :rules="rules" ref="ruleForm" label-width="100px" label-position="right">
      <el-form-item label="商品名称:" prop="goodsName">
        <el-input v-model="addForm.goodsName" placeholder="请输入商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品原价:" prop="originalPrice">
        <el-input v-model="addForm.originalPrice" placeholder="请输入商品原价">
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="销售价格:" prop="sellPrice">
        <el-input v-model="addForm.sellPrice" placeholder="请输入销售价格">
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="商品库存:" prop="stock">
        <el-input v-model="addForm.stock" placeholder="请输入商品库存">
          <template slot="append">件</template>
        </el-input>
      </el-form-item>
      <el-form-item label="限购数量:" prop="limitQuantity">
        <el-radio-group v-model="addForm.isLimitQuantity" @change="changeisLimit" style="width: 200px;">
          <p style="margin: 10px 0;">
            <el-radio :label="0">不限购</el-radio>
          </p>
          <el-radio :label="1">
            <el-input v-model="addForm.limitQuantity" :disabled="addForm.isLimitQuantity == 0" size="mini" style="width: 120px;">
              <template slot="append">个/人</template>
            </el-input>
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="商品图片:" prop="imageList" style="display: block;">
        <div class="uploadbtn" @click="uploadBtn">
          <input type="file" @change="upload" multiple ref="file" style="display: none;" />
          <img :src="addForm.imageList[0]">
          <i class="el-icon-upload" v-if="addForm.imageList[0].url == null" />
        </div>
      </el-form-item>

      <el-form-item label="商品介绍:" prop="description">
        <el-input maxlength="63" v-model="addForm.description" type="textarea" rows="4" style="width: 500px;"></el-input>
      </el-form-item>
    </el-form>

    <p style="text-align: center;">
      <el-button @click="$emit('closeDialog')">取消</el-button>
      <el-button type="primary" @click="confirm('ruleForm')">确定</el-button>
    </p>


  </div>
</template>

<script>
  export default {
    props: ['id'],
    data() {
      return {
        radio: 3,
        addForm: {
          imageList: ['url:null'],
          isLimitQuantity: 0,
          limitQuantity: 0
        },
        rules: {
            goodsName: { required: true, message: '请输入商品名称', trigger: 'blur' },
            originalPrice: { required: true, message: '请输入商品原价', trigger: 'blur' },
            sellPrice: { required: true, message: '请输入销售价格', trigger: 'blur' },
            stock: { required: true, message: '请输入库存', trigger: 'blur' },
            limitQuantity: { required: true, message: '请选择限购数量', trigger: 'blur' },
            isPermanent: { required: true, message: '请选择有效期', trigger: 'blur' },
            imageList: { required: true, message: '请上传商品图片', trigger: 'blur' },
            description: { required: true, message: '请输入商品描述', trigger: 'blur' },
        }
      }
    },

    mounted() {
      this.addForm.shopId = this.id;
    },

    methods: {
      confirm(formName) {
        this.$refs[formName].validate((valid) => {
          if(valid) {
            this.$axios({
              method: 'post',
              url: '/shop/goods/add',
              data: this.$qs.stringify(this.addForm)
            }).then((res) => {
              if(res.code == 200) {
                this.$message.success('新增商品成功');
                this.$emit('closeDialog',false);
                this.$emit('refresh');
              }
            })
          }
        })
      },

      uploadBtn(){
        this.$refs.file.dispatchEvent(new MouseEvent('click'))
      },

      changeisLimit(e) {
        this.addForm.limitQuantity = e == 0 ? 0 : '';
      },

      upload(e) {
        let files = e.target.files
        this.$nextTick(() => {
            // this.option.img = URL.createObjectURL(files[0]);
            // this.fileinfo = files[0].name;
            // this.dialogVisible = true
        })
        for (let i = 0; i < files.length; i++) {
          const isImg = files[i].type.indexOf('image') > -1
          const isLimit = files[i].size / 1024 / 1024 < 10

          if (!isImg) {
            this.$message.error('请上传图片格式!')
            return false
          }
          if (!isLimit) {
            this.$message.error('大小不能超过 10MB!')
            return false
          }
          let formData = new FormData()
          formData.append('files', files[i])

          var image = { url: null, percent: 0 }
          // this.imageList.push(image)

          this.uploadAxios(formData, image)
        }
      },

      uploadAxios(formData, image){
        this.$axios({
          url: 'https://file.zmzncs.com/upload/upload-batch',
          method: 'post',
          data: formData,
          timeout: 1000 * 60, // 请求超时时间
          onUploadProgress: (progress)=> {
            image.percent = Math.round(progress.loaded / progress.total * 100)
          }
        }).then((res) => {
          if (res.code == 200) {
            this.addForm.imageList = [];
            this.addForm.imageList.push(res.data[0].url)
            image.percent = 100
            image.url = res.data[0].url
            // this.imgs.push(image.url)
            //  组件返回值
            // var urlList = []
            // this.imgs = [];
            // this.imageList.forEach((image)=> {
            //   this.imgs.push(image.url)
            // })
            // var urls = urlList.toString()
            // this.$emit('input', urls)
          }
        }).catch(err => {})
      },
    }
  }
</script>

<style scoped>
  .el-form-item {
    display: inline-block;
  }

  .el-input {
    width: 200px;
  }

  .uploadbtn {
    border: 1px solid #aaa;
    height: 100px;
    width: 100px;
    position: relative;
  }

  .uploadbtn img {
    max-width: 100%;
    max-height: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }

  .el-icon-upload {
    font-size: 50px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }





</style>
